package com.luocc.tim.recycler;

import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.luocc.tim.MainActivity;
import com.luocc.tim.R;
import com.luocc.tim.TimApplication;
import com.luocc.tim.entity.Message;
import com.luocc.tim.ui.ChatInterfaceFragment;
import com.luocc.tim.utils.TimConstants;
import com.luocc.tim.utils.TimUtils;
import com.luocc.tim.viewholder.DefaultHolder;
import com.luocc.tim.viewholder.MessageAbstractHolder;
import com.luocc.tim.viewholder.RefreshHolder;
import com.luocc.tim.viewholder.SearchHolder;

import java.util.List;

public class ChatListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final Context mContext;
    private final List<Message> mMessages;
    private static final String TAG = "MessageListAdapter";

    public ChatListAdapter(Context context, List<Message> messages) {
        mContext = context;
        mMessages = messages;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view;
        switch (viewType) {
            case TimConstants.ItemType.ITEM_TYPE_REFRESH:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_refresh, parent, false);
                return new RefreshHolder(view);
            case TimConstants.ItemType.ITEM_TYPE_SEARCH:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_search, parent, false);
                return new SearchHolder(view);
            case TimConstants.ItemType.ITEM_TYPE_MESSAGE_ABSTRACT:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_chat, parent, false);
                return new MessageAbstractHolder(view);
            default:
                view = LayoutInflater.from(mContext).inflate(R.layout.layout_empty, parent, false);
                return new DefaultHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SearchHolder) {
            Log.d(TAG, "onBindViewHolder: SearchHolder position: " + position);
        } else if (holder instanceof MessageAbstractHolder) {
            MessageAbstractHolder messageAbstractHolder = (MessageAbstractHolder) holder;
            Message message = mMessages.get(position);
            String target = TimApplication.getSelf().equals(message.getSender()) ? message.getReceiver() : message.getSender();
            messageAbstractHolder.mImage.setBackgroundResource(R.mipmap.photo_boy);
            messageAbstractHolder.mLeft.setOnClickListener(v -> {
                ChatInterfaceFragment fragment = new ChatInterfaceFragment();
                Bundle bundle = new Bundle(1);
                bundle.putString("target", target);
                fragment.setArguments(bundle);

                FragmentManager fragmentManager = ((MainActivity) mContext).getSupportFragmentManager();
                FragmentTransaction transaction = fragmentManager.beginTransaction();
                /*
                    四个参数解释：
                        1.点击选项时，要显示的fragment
                        2.点击选项时，要隐藏的fragment
                        3.点击返回时，要显示的fragment
                        4.点击返回时，要隐藏的fragment
                 */
                transaction.setCustomAnimations(R.anim.to_left_enter, R.anim.to_left_exit, R.anim.to_right_pop_enter, R.anim.to_right_pop_exit);
                transaction.addToBackStack(null);
                transaction.replace(R.id.container, fragment, "CHAT_FRAGMENT");
                transaction.commit();
            });
            messageAbstractHolder.mDelete.setOnClickListener(v -> {
                holder.itemView.scrollTo(0, 0);
                Log.d(TAG, "onBindViewHolder: message delete clicked : " + position);
                mMessages.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position, mMessages.size());
            });
            messageAbstractHolder.mTarget.setText(target);
            messageAbstractHolder.mContent.setText(message.getContent());
            messageAbstractHolder.mTime.setText(TimUtils.getDateString(message.getDate()));
        }
    }

    @Override
    public int getItemCount() {
        return mMessages.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (getItemCount() == 0) {
            return TimConstants.ItemType.ITEM_TYPE_EMPTY;
        } else if (mMessages.get(position) != null){
            return mMessages.get(position).getType();
        } else {
            return super.getItemViewType(position);
        }
    }
}
